package gui;

import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import negocios.Classificacao;
import negocios.Fachada;

public class CadastraEditaClassificacoes extends JDialog {


	private static final long serialVersionUID = 1L;
	
	private Classificacao classificacao = null;
	
    private JLabel labDescricao = null;
     
    private JTextField txfDescricao = null;
     
    private JButton btConfirmar = null;
    
    private Border labelBorder = null;

    public CadastraEditaClassificacoes(Frame owner) {

        super(owner);
        initialize();

    }
    
    public CadastraEditaClassificacoes(Frame owner, Classificacao cls) {

        super(owner);
        initialize();
        this.classificacao = cls;

    }

    private void initialize() {

        this.setLayout(null);
        this.setTitle("Cadastra/Edita Classificações");
        this.setSize(280, 130);
        this.setModal(true);
        
        labelBorder = BorderFactory.createLineBorder(Color.BLACK);

        this.add(getLabDescricao(), null);
        
        this.add(getTxfDescricao(), null);
        
        this.add(getBtConfirmar(), null);

    }

    private JLabel getLabDescricao() {

        if (labDescricao == null) {

            labDescricao = new JLabel();
            labDescricao.setBounds(20, 20, 80, 30);
            labDescricao.setHorizontalAlignment(JLabel.CENTER);
            labDescricao.setBorder(labelBorder);
            labDescricao.setText("Descrição");

        }

        return labDescricao;

    }    

    private JTextField getTxfDescricao() {
    
    	if (txfDescricao == null) {
    		
    		txfDescricao = new JTextField();
    		txfDescricao.setBounds(110, 20, 150, 30);
    		
    		if (this.classificacao != null) txfDescricao.setText(this.classificacao.getDescricao());
    		
    	}
    	
    	return txfDescricao;
    	
    }
    
   
    private JButton getBtConfirmar() { 
    
    	if (btConfirmar == null) {
    		
    		btConfirmar = new JButton();
    		btConfirmar.setText("Confirmar");
    		btConfirmar.setBounds(170, 60, 90, 30);
            btConfirmar.addActionListener(new ActionListener() {

                @Override public void actionPerformed(ActionEvent event) {

                        if (classificacao == null) {
                                                        
                            classificacao = new Classificacao(0, getTxfDescricao().getText());
                            
                            Fachada.getInstancia().inserirClassificacao(classificacao);

                        } else {

                            classificacao.setDescricao(getTxfDescricao().getText());
                            Fachada.getInstancia().editarClassificacao(classificacao);

                        }
                        
                        
                    }

                }

            );
    		
    	}
    	
    	return btConfirmar;
    
    }
    
}
